/* Check JER_BM_To_old_schedule_Dumb(). */
#include <stdio.h>
#include "jerasure.h"
#include "reed_sol.h"

int main( int argc, char** argv )
{
	int *old_bitmatrix;
	int *old_matrix;
	int** old_schedule;
	int m = atoi( argv[1] );
	int k = atoi( argv[2] );
	int w = atoi( argv[3] );
	int i;
	int size;
	unsigned char* buffer;
	char** data_ptrs;
	char** coding_ptrs;
	char** all_ptrs;

	size = 8 * w;
	
	srand48(m);
	buffer = (unsigned char*) malloc( sizeof(unsigned char) * (k+m) * size );
	data_ptrs = (char**) malloc( sizeof( char* ) * k );
	coding_ptrs = (char**) malloc( sizeof( char* ) * m );
	all_ptrs = (char**) malloc( sizeof( char* ) * (k+m) );
	for( i = 0; i < k * size; i++ )
	{
		buffer[i] = lrand48() % 256;
		if( i % size == 0 )
			fprintf( stdout, "\n" );
		fprintf( stdout, "%x", buffer[i] );
	}
	fprintf( stdout, "\n" );
	for( i = 0; i < k; i++ )
	{
		data_ptrs[i] = buffer + i*size;
		all_ptrs[i] = data_ptrs[i];
	}
	for( i = 0; i < m; i++ )
	{
		coding_ptrs[i] = buffer + k * size + i * size;
		all_ptrs[k+i] = coding_ptrs[i];
	}

	old_matrix = reed_sol_vandermonde_coding_matrix( k, m, w );
	old_bitmatrix = jerasure_matrix_to_bitmatrix( k, m, w, old_matrix );
	if( old_bitmatrix != NULL )
		jerasure_print_bitmatrix( old_bitmatrix, m*w, k*w, w );
	old_schedule = jerasure_smart_bitmatrix_to_schedule( k, m, w, old_bitmatrix );

	jerasure_schedule_encode( k, m, w, old_schedule, data_ptrs, coding_ptrs,
								size, 8 );

	for( i = 0; old_schedule[i][0] != -1; i++ )
	{
		fprintf( stdout, "<%d,%d,%d,%d,%d>\n", old_schedule[i][4], 
				old_schedule[i][0], old_schedule[i][1], old_schedule[i][2], 
				old_schedule[i][3] );
	}

	for( i = 0; i < (k+m) * size; i++ )
	{
		if( i % size == 0 )
			fprintf( stdout, "\n" );
		fprintf( stdout, "%x", buffer[i] );
	}
	fprintf( stdout, "\n" );

	return 0;
}
